// pages/ewmCode.js
var app = getApp();
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height:"",
    url:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight,"height")
        that.setData({
          height:res.windowHeight + 20
        })
      }
    })
    that.getList()
  },
  //保存至相册
  saveImageToPhotosAlbum: function () {
    // 保存图片
    let that = this;
    wx.getSetting({
        success: function(res){
            //不存在相册授权
            if (!res.authSetting['scope.writePhotosAlbum']){
              wx.openSetting({
                success:()=>{
                
                }
            })
                wx.authorize({
                    scope: 'scope.writePhotosAlbum',
                    success: function(){
                        that.savePhoto();
                    },
                    fail: function(err){
                        wx.showToast({
                            title: '授权失败',
                          })
                        }
                })
            }else{
                that.savePhoto(); 
            }
        }
    })

},
savePhoto: function () {
    const ctx = wx.createCanvasContext('myCanvas')
    wx.getImageInfo({
      src: this.data.url,
      success(res) {
        ctx.drawImage("../../images/index/canvas.png", 0, 0, wx.getSystemInfoSync().windowWidth, 667)
        ctx.drawImage(res.path, (wx.getSystemInfoSync().windowWidth - 220) / 2, 320, 220, 220)
        console.log(ctx.drawImage)
        ctx.draw(true, setTimeout(function () {
          wx.canvasToTempFilePath({
            canvasId: 'myCanvas',
            success: function (res) {
              console.log(res)
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success: () => {
                  wx.showToast({
                    title: '保存成功',
                  })
                },
                fail: () => {
                  wx.showToast({
                    title: '保存失败',
                  })
                }
              })
            }
          })
        }, 100))
      }
    })

  },
    // 二维码图片数据
    getList:function() {
      var that = this
      util.https(app.globalData.api + "/applet/partner/getInviteCode", "GET", {},
      function (data) {
        console.log(data)
          if (data.code == 200) {
              that.setData({
                  url: data.data
              })
          } else {
              util.toolTip(that, data.message)
          }
      }
  )
    },
})